package com.ukao.cashregister.printer;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.util.Log;

import com.example.tscdll.TSCUSBActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.cashregister.R;
import com.ukao.cashregister.bean.AllMarkprintBean;
import com.ukao.cashregister.bean.MarkprintBean;
import com.ukao.cashregister.bean.PrinterBean;
import com.ukao.cashregister.bean.StrikeBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.eventbus.MembershipCardEvent;
import com.ukao.cashregister.listener.OnPrintListener;
import com.ukao.cashregister.usbrfidreader.UsbrfidreaderHelper;
import com.ukao.cashregister.utils.CheckUsbDeviceUtils;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.MyDateUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.ThreadUtils;
import com.ukao.cashregister.utils.Utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;



/**
 * Created by herego on 2017/6/26.
 * 注意打印机需要刷字体
 * Font001 : 新宋体  小五
 * Font002:  新宋体  四号
 */
public class PrinterHelper       {
    private static PrinterHelper mPrinterHelper = null;
    private UsbManager mUsbManager = null;
    private List<StringBean> mAnnexDesclist;
    private  int count;


    private int countall;
    private  AllMarkprintBean  mClotbean;
    private List<MarkprintBean.AnnexListBean> productList;
    private List<AllMarkprintBean.ProductListBean.AnnexListBean> productLists;
    private String paystate;
    private String productName="";
    private  Handler  mHandler=new Handler();

    private PrinterHelper( ){
        mUsbManager=(UsbManager)Utils.getContext().getSystemService(Context.USB_SERVICE);
    }


    public static PrinterHelper getInstance( ){
        if(mPrinterHelper == null) {
            mPrinterHelper = new PrinterHelper();
        }
        return  mPrinterHelper;
    }


    public byte[] convertGBK(String str){
        byte[] retBytes = null;
        try {
            retBytes = str.getBytes("GBK");
        }catch (UnsupportedEncodingException ex){
            ex.printStackTrace();
        }
        return retBytes;
    }

    public UsbDevice getSpecialPrinter(int vendorId, int productId){

        mUsbManager=(UsbManager)Utils.getContext().getSystemService(Context.USB_SERVICE);

        if(mUsbManager != null) {
            HashMap<String, UsbDevice> deviceHashMap = mUsbManager.getDeviceList();
            Iterator<UsbDevice> iterator = deviceHashMap.values().iterator();

            while (iterator.hasNext()) {
                UsbDevice   device= iterator.next();
                int mVendorId = device.getVendorId();
                int mmProductId = device.getProductId();
                L.i( "vendor id:" +mVendorId+ "  mProductId: " +mmProductId );
                if (mVendorId==vendorId && mmProductId == productId) {
                    return device;
                }
            }
        }else {
            Log.i("Debug:","-------------usbManager is null");
        }
        return null;
    }




    public void printTsc247(PrintInfo printInfo  ,final OnPrintListener   mOnPrintListener){
        UsbDevice  usbDevice=null;
        if (CheckUsbDeviceUtils.isT210EWashprinter()){
            usbDevice = getSpecialPrinter(4611,310);

        }else{
            usbDevice = getSpecialPrinter(4611,294);
        }

        if(null==usbDevice){
            System.out.println("usbDevice is null");
            return;
        }

        try {
            TSCUSBActivity tscusbActivity = new TSCUSBActivity();
            try {
                tscusbActivity.openport(mUsbManager, usbDevice);
            }catch (Exception  e){
                T.show(Utils.getContext().getString(R.string.default_use_usb_device_inot_set));

                return;
            }
            tscusbActivity.sendcommand("SIZE 110 mm,15 mm\n");
            tscusbActivity.sendcommand("GAP 0 mm,0 mm\n");
            tscusbActivity.sendcommand("DIRECTION 1\n");
            tscusbActivity.sendcommand("CLS\n");
            tscusbActivity.sendcommand("SET CUTTER ON\n");
            if (printInfo.isaffix){  /*是否是衣物*/
                tscusbActivity.sendcommand(convertGBK("TEXT 65,85,\"FONT001\",0,2,2,\""+printInfo.mAttachInfo+"\"\n"));
            }else{

                tscusbActivity.sendcommand(convertGBK("TEXT 65,85,\"FONT001\",0,3,3,\""+ printInfo.mAttachInfo+"\"\n"));
            }
            tscusbActivity.sendcommand(convertGBK("TEXT 20,38,\"FONT001\",0,2,2,\""+printInfo.mCurrentIndex+"\"\n"));/*当前数*/
            tscusbActivity.sendcommand(convertGBK("TEXT 20,85,\"FONT001\",0,2,2,\""+printInfo.mTotalCount+"\"\n"));/*总数*/
            tscusbActivity.sendcommand(convertGBK("TEXT 20,8,\"FONT001\",0,2,2,\""+printInfo.mGoodsName+"\"\n"));/*衣物*/
            tscusbActivity.sendcommand(convertGBK("TEXT 340,8,\"FONT001\",0,2,2,\""+printInfo.mOrderInfo+"\"\n"));/*订单号*/
           tscusbActivity.sendcommand(convertGBK("TEXT 65,35,\"FONT001\",0,2,2,\""+printInfo.mcleanout+"\"\n"));/*清洗*/
           if (printInfo.annexDescCount!=0){
               tscusbActivity.sendcommand(convertGBK("TEXT 65,60,\"FONT001\",0,2,2,\"附件："+printInfo.mAttachGoods+"\"\n"));/*附件数量*/
           }
            tscusbActivity.sendcommand(convertGBK("TEXT 20,65,\"FONT001\",0,2,2,\"-\"\n"));


            printInfo.mTransportName=CheckUtils.isEmptyStringLeng(printInfo.mTransportName,1);
           String   time=printInfo.mClothesBarcode+" "+MyDateUtils.formatDateCustom(printInfo.createTime,"MM.dd");
            tscusbActivity.sendcommand(convertGBK("QRCODE 490,8,H,4,A,0,M2,\""+printInfo.mClothesBarcode+"\"\n")); /*二维码*/
            tscusbActivity.sendcommand(convertGBK("TEXT 590,20,\"FONT001\",0,2,2,\""+printInfo.mShopName+"\"\n"));/*店铺*/
            tscusbActivity.sendcommand(convertGBK("TEXT 810,20,\"0\",0,14,13,\""+printInfo.mShopNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 590,60,\"FONT001\",0,2,2,\""+time+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 490,90,\"FONT001\",0,2,2,\""+printInfo.Use+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 730,90,\"FONT001\",0,2,2,\""+printInfo.mTransportName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 750,90,\"0\",0,14,13,\""+printInfo.mTransportNum+"\"\n"));/*输送线号*/
            tscusbActivity.sendcommand("PRINT 1\n");

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mOnPrintListener.onPrintSucced();
                }
            }, 600);

        }catch (Exception ex){
            T.show(Utils.getContext().getString(R.string.printer_error));


        }
    }







    /**
     * 单独打标
     * @param mClotbean
     */
    MarkprintBean mthoClotbean;
    public   void   printWashing(final MarkprintBean mClotbean){

            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    alonePrintWashing(mClotbean);
                }
            });





    }

    public void alonePrintWashing(MarkprintBean mClotbean) {
        this.mthoClotbean=mClotbean;
        final PrintInfo printInfo = new PrintInfo();

        Gson mGson = new Gson();
        String color="";
        if (!CheckUtils.isEmpty(mClotbean.getColorDesc())) {  /*/*瑕疵*/
            List<StringBean> mColorDesclist = mGson.fromJson(mClotbean.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mColorBuffer1 = new StringBuffer();
            for (int i = 0; i < mColorDesclist.size(); i++) {
                if (i != mColorDesclist.size() - 1) {
                    mColorBuffer1.append(mColorDesclist.get(i).getName() + "/");
                } else {
                    mColorBuffer1.append(mColorDesclist.get(i).getName());
                }


            }
            color= mColorBuffer1.toString();
        }
        String strflawDesc = "";
        if (!CheckUtils.isEmpty(mClotbean.getFlawDesc())) {  /*/*瑕疵*/
            List<StringBean> mflawDesclist = mGson.fromJson(mClotbean.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mflawBuffer = new StringBuffer();
            for (int i = 0; i < mflawDesclist.size(); i++) {
                if (i != mflawDesclist.size() - 1) {
                    mflawBuffer.append(mflawDesclist.get(i).getName() + "/");
                } else {
                    mflawBuffer.append(mflawDesclist.get(i).getName());
                }


            }
            strflawDesc = mflawBuffer.toString();
        }
        String annexDesc = "";
        int annexDescCount=0;
        if (!CheckUtils.isEmpty(mClotbean.getAnnexDesc())){  /*附件*/
             mAnnexDesclist= mGson.fromJson(mClotbean.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount=annexDescCount+1;
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annexDesc = mStringBuffer3.toString();

        }

        String service = "";
        if (!CheckUtils.isEmpty(mClotbean.getAddServiceDesc())){  /* 附加  */
            List<StringBean> mServiceDesclist = mGson.fromJson(mClotbean.getAddServiceDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mserviceBuffer = new StringBuffer();
            for (int i = 0; i < mServiceDesclist.size(); i++) {
                StringBean  item=mServiceDesclist.get(i);
                if (i==0){
                    mserviceBuffer.append("/");
                }
                if (i != mServiceDesclist.size() - 1) {
                    mserviceBuffer.append(item.getName()+ "/");
                } else {
                    mserviceBuffer.append(item.getName());
                }


            }
            service=mserviceBuffer.toString();

        }


        String  subtotal="  "+CheckUtils.isEmptyNumber(mClotbean.getSubtotal());/*小计*/
        String    paystate=mClotbean.isPaid() ? "  已" :"  未";

        if (mClotbean.getType()){
            printInfo.mGoodsName =  CheckUtils.isEmptyStringLeng(mClotbean.getProductName(),7)+subtotal+paystate;
            printInfo.mAttachInfo =color+"  "+CheckUtils.isEmptyStringLeng(strflawDesc,12);/*瑕疵*//*衣物*/
        }else{

            printInfo.mGoodsName =  CheckUtils.isEmptyStringLeng(mClotbean.getParentProName(),7)+subtotal+paystate;
            printInfo.mAttachInfo="附件:"+mClotbean.getProductName();
        }
        printInfo.mOrderInfo = "*"+mClotbean.getOrderNo().substring(mClotbean.getOrderNo().length()-6);/*订单*/
        printInfo.mcleanout =mClotbean.getServiceName()+CheckUtils.isEmptyStringLeng(service,20);  /*服务与 附加服务 14*/
        printInfo.mCurrentIndex = mClotbean.getNo()+"";
        printInfo.mTotalCount = mClotbean.getProductCnt()+"";
        printInfo.annexDescCount=annexDescCount;
        printInfo.mAttachGoods =annexDescCount+CheckUtils.isEmptyStringLeng(annexDesc,11) ;  /*附件数量*/
        printInfo.mShopName =CheckUtils.isEmptyString(mClotbean.getStoreName());/*门店*/
        printInfo.mShopNum = CheckUtils.isEmptyString(mClotbean.getStoreId()+"");/*门店id*/
        printInfo.mTransportName= CheckUtils.isEmptyString(mClotbean.getTranText());/*输送线名称*/
        printInfo.mTransportNum =mClotbean.getTranNum()+""; /*输送线号*/
        String  userName=mClotbean.getUserName();
        printInfo.Use = mClotbean.getUserPhone()+CheckUtils.isEmptyStringLeng(userName,4);
        printInfo.mClothesBarcode =mClotbean.getScanCode();
        printInfo.createTime=mClotbean.getCreateTime();
        printInfo.isaffix=mClotbean.getType();/*是否是衣物*/
        productList=mClotbean.getAnnexList();
        count=-1;
        mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
            @Override
            public void onPrintSucced() {
                postDelayedPrin(printInfo);
            }

            @Override
            public void onPrintFail() {
            }
        });
    }

    public void postDelayedPrin(final PrintInfo printInfo) {
        if (!CheckUtils.isEmpty(productList)){
            count=count+1;
            if (productList.size()-1>=count) {
                printInfo.isaffix=false;
                printInfo.mAttachInfo="附件:"+productList.get(count).getProductName();
                printInfo.mClothesBarcode =productList.get(count).getScanCode();
                printInfo.mCurrentIndex=productList.get(count).getNo()+"";
                String    subtotal="  "+CheckUtils.isEmptyNumber(productList.get(count).getSubtotal());
                String  paystate=mthoClotbean.isPaid() ? "  已" :"  未";
                String productName = mthoClotbean.getProductName();
                String strproductName =CheckUtils.isEmptyStringLeng(productName, 7);
                printInfo.mGoodsName =strproductName+subtotal+paystate;/*小计*/
                mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
                    @Override
                    public void onPrintSucced() {
                        postDelayedPrin(printInfo);
                    }

                    @Override
                    public void onPrintFail() {
                        postDelayedPrin(printInfo);
                    }
                });

            }


        }
    }


    public   void   printAllWashings(final AllMarkprintBean  mClotbeans){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mClotbean=mClotbeans;
                countall=0;
                printAllWashing();
            }
        });


    }


    /**
     * 全打标
     */
    public   void   printAllWashing(  ){
        List<AllMarkprintBean.ProductListBean> allproductList = mClotbean.getProductList();
        if (CheckUtils.isEmpty(allproductList))return;
        if (allproductList.size()-1<countall)return;
        AllMarkprintBean.ProductListBean mitem = allproductList.get(countall);
        final     PrintInfo printInfo = new PrintInfo();
        Gson mGson = new Gson();
        String color="";
        if (!CheckUtils.isEmpty(mitem.getColorDesc())) {  /*/*瑕疵*/
            List<StringBean> mColorDesclist = mGson.fromJson(mitem.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mColorBuffer1 = new StringBuffer();
            for (int i = 0; i < mColorDesclist.size(); i++) {
                if (i != mColorDesclist.size() - 1) {
                    mColorBuffer1.append(mColorDesclist.get(i).getName() + "/");
                } else {
                    mColorBuffer1.append(mColorDesclist.get(i).getName());
                }


            }
            color= mColorBuffer1.toString();
        }
        String strflawDesc = "";
        if (!CheckUtils.isEmpty(mitem.getFlawDesc())) {  /*/*瑕疵*/
            List<StringBean> mflawDesclist = mGson.fromJson(mitem.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mflawBuffer = new StringBuffer();
            for (int i = 0; i < mflawDesclist.size(); i++) {
                if (i != mflawDesclist.size() - 1) {
                    mflawBuffer.append(mflawDesclist.get(i).getName() + "/");
                } else {
                    mflawBuffer.append(mflawDesclist.get(i).getName());
                }


            }
            strflawDesc = mflawBuffer.toString();
        }
        String annexDesc = "";
        int annexDescCount=0;
        if (!CheckUtils.isEmpty(mitem.getAnnexDesc())){  /*附件*/
            mAnnexDesclist= mGson.fromJson(mitem.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount=annexDescCount+1;
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annexDesc = mStringBuffer3.toString();

        }



        String service = "";
        if (!CheckUtils.isEmpty(mitem.getAddServiceDesc())){  /* 附加  */
            List<StringBean> mServiceDesclist = mGson.fromJson(mitem.getAddServiceDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mserviceBuffer = new StringBuffer();
            for (int i = 0; i < mServiceDesclist.size(); i++) {
                StringBean  item=mServiceDesclist.get(i);
                if (i==0){
                    mserviceBuffer.append("/");
                }
                if (i != mServiceDesclist.size() - 1) {
                    mserviceBuffer.append(item.getName()+ "/");
                } else {
                    mserviceBuffer.append(item.getName());
                }


            }
            service=mserviceBuffer.toString();

        }
        /*最一层*/
        printInfo.createTime=mitem.getCreateTime();
        String  userName=mClotbean.getUserName();
        printInfo.Use = mClotbean.getUserPhone()+CheckUtils.isEmptyStringLeng(userName,4);
        printInfo.mTotalCount = mClotbean.getProductCnt()+"";
        printInfo.mShopName =CheckUtils.isEmptyString(mClotbean.getStoreName());
        printInfo.mShopNum = CheckUtils.isEmptyString(mClotbean.getStoreId()+"");/*门店id*/
        printInfo.mOrderInfo = "*"+mClotbean.getOrderNo().substring(mClotbean.getOrderNo().length()-6);/*订单*/
        /*第二层*/
        String   subtotal=CheckUtils.isEmptyNumber(mitem.getSubtotal());
          paystate=mClotbean.isPaid() ? "  已" :"  未";
        productName=CheckUtils.isEmptyStringLeng(mitem.getProductName(),7);
        printInfo.mGoodsName = productName+subtotal+paystate;/*小计*/
        printInfo.mCurrentIndex = mitem.getNo()+"";
        printInfo.isaffix=mitem.getType()==1?true :false;/*是否是衣物*/
        printInfo.mcleanout =mitem.getServiceName()+CheckUtils.isEmptyStringLeng(service,14);  /*服务与 附加服务*/
        printInfo.mTransportNum =mitem.getTranNum()+""; /*输送线*/
        printInfo.mClothesBarcode =mitem.getScanCode();/*二维码*/
        printInfo.annexDescCount=annexDescCount;
        printInfo.mTransportName= CheckUtils.isEmptyString(mitem.getTranText());
        printInfo.mAttachGoods =annexDescCount+CheckUtils.isEmptyStringLeng(annexDesc,12) ;  /*附件*/
        printInfo.mAttachInfo =color+"  "+CheckUtils.isEmptyStringLeng(strflawDesc,12);/*瑕疵*//*衣物*/
        count=-1;
        productLists= mitem.getAnnexList();
        mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
            @Override
            public void onPrintSucced() {
                if (CheckUtils.isEmpty(productLists)){ /*没附件*/
                    countall=countall+1;
                    printAllWashing();

                }else{
                    printAttachment(printInfo);
                }

            }

            @Override
            public void onPrintFail() {
                if (CheckUtils.isEmpty(productLists)){ /*没附件*/
                    countall=countall+1;
                    printAllWashing();

                }else{
                    printAttachment(printInfo);
                }
            }
        });


    }

    public void printAttachment(final PrintInfo printInfo) {
    /*附件集合*/
        count=count+1;
        if (productLists.size()-1>=count) {
            printInfo.isaffix=false;
            printInfo.mAttachInfo="附件:"+productLists.get(count).getProductName();
            printInfo.mClothesBarcode =productLists.get(count).getScanCode();
            printInfo.mCurrentIndex=productLists.get(count).getNo()+"";
            String    subtotal=" "+ CheckUtils.isEmptyNumber(productLists.get(count).getSubtotal());
            paystate=mClotbean.isPaid() ? "  已" :"  未";
            printInfo.mGoodsName =productName+subtotal+paystate;/*小计*/
            mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
                @Override
                public void onPrintSucced() {
                    printAttachment(printInfo);

                }

                @Override
                public void onPrintFail() {
                    printAttachment(printInfo);
                }
            });

        }else {
            countall=countall+1;
            printAllWashing();
        }
    }



    /**
     * 获取打印设备
     * @return
     */
    public List<PrinterBean>   loadPrinterList(  ) {

        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        StringBuilder sb = new StringBuilder();
        List<PrinterBean>  mData=new ArrayList<>();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevice = deviceIterator.next();
            sb.append("DeviceName=" + usbDevice.getDeviceName() + "\n");
            sb.append("DeviceId=" + usbDevice.getDeviceId() + "\n");
            sb.append("VendorId=" + usbDevice.getVendorId() + "\n");
            sb.append("ProductId=" + usbDevice.getProductId() + "\n");
            sb.append("DeviceClass=" + usbDevice.getDeviceClass() + "\n");
            int deviceClass = usbDevice.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevice.getInterface(0);
                int interfaceClass = anInterface.getInterfaceClass();

                sb.append("device Class 为0-------------\n");
                sb.append("Interface.describeContents()=" + anInterface.describeContents() + "\n");
                sb.append("Interface.getEndpointCount()=" + anInterface.getEndpointCount() + "\n");
                sb.append("Interface.getId()=" + anInterface.getId() + "\n");
                //http://blog.csdn.net/u013686019/article/details/50409421
                //http://www.usb.org/developers/defined_class/#BaseClassFFh
                //通过下面的InterfaceClass 来判断到底是哪一种的，例如7就是打印机，8就是usb的U盘
                sb.append("Interface.getInterfaceClass()=" + anInterface.getInterfaceClass() + "\n");
                if (anInterface.getInterfaceClass() == 7) {
                    sb.append("此设备是打印机\n");
                    if (usbDevice.getVendorId() == 34918 && usbDevice.getProductId() == 256) {
                        String	washPrint=Utils.getContext().getResources().getString(R.string.receipt_print);
                        String	washcode=Utils.getContext().getResources().getString(R.string.receipt_code);
                        mData.add(new PrinterBean(washPrint,washcode,false));

                    }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 294){/*水洗唛打印机*/
                        String	washPrint=Utils.getContext().getResources().getString(R.string.wash_print);
                        String	washcode=Utils.getContext().getResources().getString(R.string.wash_code);
                        mData.add(new PrinterBean(washPrint,washcode,true));
                    }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 370) {/*TSC清单打印*/
                        String	washcode=Utils.getContext().getResources().getString(R.string.tag_code);
                        String	washPrint=Utils.getContext().getResources().getString(R.string.tag_print);
                        mData.add(new PrinterBean(washPrint,washcode,false));

                    }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 310) {/*TSC T-210E清单打印*/

                        String	washcode=Utils.getContext().getResources().getString(R.string.detailed);
                        String	washPrint=Utils.getContext().getResources().getString(R.string.tag_print);
                        mData.add(new PrinterBean(washPrint,washcode,false));
                    }

                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此设备是U盘\n");
                }

            }




        }
        L.i("PrinterBean="+sb.toString());

        return   mData;
    }
    public  void  onDestroy(){
        mHandler.removeCallbacksAndMessages(null);
    }



    /**
     * 合格标
     * @param mStrikeBean
     */
    public  void  printStrike(StrikeBean mStrikeBean){
        PrintInfo printInfo = new PrintInfo();
        printInfo.mGoodsName = mStrikeBean.getProductName();
        printInfo.mOrderNum = mStrikeBean.getOrderNo();
        printInfo.mTotalCount = mStrikeBean.getProductCnt()+"";
        printInfo.mReciveClothesDatetime = MyDateUtils.formatDateCustom(mStrikeBean.getCreateTime(),"MM-dd HH:mm:ss");
        printInfo.mClothesBarcode =mStrikeBean.getScanCode();
        printInfo.mShopName =mStrikeBean.getStoreName();
        printInfo.mShopNum = mStrikeBean.getStoreId()+"";
        printInfo.mTransportNum = mStrikeBean.getTranNum()+"";
        printInfo.mTransportName=mStrikeBean.getTranText()+"";
        printTsc244(printInfo);
    }
    public void printTsc244(PrintInfo printInfo){
        UsbDevice usbDevice = getSpecialPrinter(4611,370);
        if(usbDevice == null){
            System.out.println("usbDevice is null");
            return;
        }

        try {
            TSCUSBActivity tscusbActivity = new TSCUSBActivity();
            try {
                tscusbActivity.openport(mUsbManager, usbDevice);
            }catch (Exception  e){
                T.show(Utils.getContext().getString(R.string.default_use_usb_device_inot_set));
                return;
            }

            tscusbActivity.sendcommand("SIZE 77.5 mm, 40 mm\n");
            tscusbActivity.sendcommand("DIRECTION 0,0\n");
            tscusbActivity.sendcommand("REFERENCE 0,0\n");
            tscusbActivity.sendcommand("DIRECTION 1\n");
            //tscusbActivity.sendcommand("SET RIBBON OFF\n"); /**/  ON 热转印模式      OFF热感印模式
            tscusbActivity.sendcommand("CLS\n");
            tscusbActivity.sendcommand("GAP 3 mm,0 mm\n");
            tscusbActivity.sendcommand("OFFSET 0 mm\n");
            tscusbActivity.sendcommand("SET PEEL OFF\n");
            tscusbActivity.sendcommand("SET CUTTER OFF\n");
            tscusbActivity.sendcommand("SET PARTIAL_CUTTER OFF\n");
//       tscusbActivity.sendcommand(convertGBK("BARCODE 20,110,\"128\",1,0,0,2,2,\""+printInfo.mClothesBarcode+"\"\n"))  / 第一个x y  *128高度*/
            tscusbActivity.sendcommand(convertGBK("TEXT 5,10,\"FONT002\",0,1,1,\""+printInfo.mGoodsName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 5,30,\"FONT002\",0,1,1,\"单号："+printInfo.mOrderNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 5,50,\"FONT002\",0,1,1,\"总数："+printInfo.mTotalCount+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 5,70,\"FONT002\",0,1,1,\"收衣时间："+printInfo.mReciveClothesDatetime+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("BARCODE 1,110,\"128\",120,0,0,2,2,\""+printInfo.mClothesBarcode+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 5,230,\"FONT001\",0,2,2,\""+printInfo.mClothesBarcode+"\"\n"));

            tscusbActivity.sendcommand(convertGBK("TEXT 290,120,\"FONT001\",0,2,2,\"合\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 290,150,\"FONT001\",0,2,2,\"格\"\n"));
            tscusbActivity.sendcommand(convertGBK("BOX 285,110,325,180,2\n"));
            if (printInfo.mShopName.length()>8){
                printInfo.mShopName=printInfo.mShopName.substring(0,8)+"...";

            }

            tscusbActivity.sendcommand(convertGBK("TEXT 300,26,\"FONT002\",0,1,1,\""+printInfo.mShopName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 505,10,\"FONT002\",0,2,2,\""+printInfo.mShopNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 445,66,\"FONT002\",0,1,1,\""+printInfo.mTransportName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 505,50,\"FONT002\",0,2,2,\""+printInfo.mTransportNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("BARCODE 370,110,\"128\",120,0,0,2,1,\""+printInfo.mClothesBarcode+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 370,230,\"FONT001\",0,2,2,\""+printInfo.mClothesBarcode+"\"\n"));


            tscusbActivity.sendcommand("PRINT 1\n");
        }catch (Exception ex){
            T.show(Utils.getContext().getString(R.string.printer_error));
        }
    }

}
